<?php

namespace App\Models;

use App\Http\Controllers\AccessSystem\CommonController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * 数字资源书单推荐书籍表
 */
class ResourceReadingListBook extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_reading_list_book';




    /**
     * 获取书单书籍列表
     * @param limit int 分页大小
     * @param reading_id 书单id
     * @param big_type_id 大类id
     * @param keywords string 搜索关键词
     */
    public function lists($search_data = [])
    {
        $reading_id = $search_data['reading_id'] ?? '';
        $big_type_id = $search_data['big_type_id'] ?? '';
        $keywords = $search_data['keywords'] ?? '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;
        //获取书单数据
        $resourceReadingListModel = new ResourceReadingList();
        $reading_info = $resourceReadingListModel->detail($reading_id);
        if (empty($reading_info)) {
            return [];
        }

        $resourceTotalModel = new ResourceTotal();
        $field = ['r.id', 'r.reading_id', 'r.resource_id', 'r.create_time', 'resource_business_id', 'guid', 'title', 'author', 'press', 'pre_time', 'isbn', 'img', 'intro', 't.browse_num'];

        //获取敏感词
        // $sensitiveWordModel = new SensitiveWord();
        // $sensitive_word = $sensitiveWordModel->getSensitiveWordALL();

        $res = $this->from($this->getTable() . " as r")->select($field)
            ->join($resourceTotalModel->getTable() . ' as t', 't.id', '=', 'r.resource_id')
            ->join('resource_business_name as n', 'n.id', '=', 't.resource_business_id')
            ->where('reading_id', $reading_id)
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('title', 'like', "%$keywords%")->Orwhere('author', 'like', "%$keywords%");
                }
            })
            ->where(function ($query) use ($big_type_id) {
                if ($big_type_id) {
                    $query->where('t.big_type_id', $big_type_id);
                }
            })
            // ->where(function ($query) use ($sensitive_word) {
            //     if ($sensitive_word) {
            //         foreach ($sensitive_word as $item) {
            //             $query->where('title', 'not like', "%$item%");
            //         }
            //     }
            // })->where(function ($query) use ($sensitive_word) {
            //     if ($sensitive_word) {
            //         foreach ($sensitive_word as $item) {
            //             $query->where('author', 'not like', "%$item%");
            //         }
            //         $query->orWhereNull('author');
            //     }
            // })
            ->where('is_mask', 1)
            ->where('t.is_del', 1)
            ->where('n.is_del', 1)
            ->orderByDesc('r.id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }

    /**
     *获取书单书籍数量
     * @param reading_id 书单id
     */
    public function getResourceReadingListBookCount($reading_id, $big_type_id = null)
    {
        $resourceTotalModel = new ResourceTotal();
        $count = $this->from($this->getTable() . " as r")->select('r.id')
            ->join($resourceTotalModel->getTable() . ' as t', 't.id', '=', 'r.resource_id')
            ->join('resource_business_name as n', 'n.id', '=', 't.resource_business_id')
            ->where(function ($query) use ($big_type_id) {
                if ($big_type_id) {
                    $query->where('t.big_type_id', $big_type_id);
                }
            })
            ->where('reading_id', $reading_id)
            ->where('t.is_del', 1)
            ->where('n.is_del', 1)
            ->count();
        return $count;
    }

    /**
     * 添加系统类型作品
     * @param reading_id int 书单id
     * @param resource_ids int 系统类型作品的id 多个逗号拼接
     */
    public function addResourceReadingListBook($reading_id, $resource_ids)
    {
        $old_resource_ids = $this->where('reading_id', $reading_id)->pluck('resource_id')->toArray();
        $resource_ids = explode(',', $resource_ids);
        foreach ($resource_ids as $key => $val) {
            if (!in_array($val, $old_resource_ids)) {
                $obj = new self();
                $obj->reading_id = $reading_id;
                $obj->resource_id = $val;
                $obj->save();
            }
        }
        return true;
    }


    /**
     * 删除系统类型作品
     * @param reading_id int 书单id
     * @param reading_list_resource_ids  数据id 多个逗号拼接
     */
    public function delResourceReadingListBook($reading_id, $reading_list_resource_ids)
    {
        $reading_list_resource_ids = explode(',', $reading_list_resource_ids);
        return $this->where('reading_id', $reading_id)->whereIn('id', $reading_list_resource_ids)->delete();
    }
    /**
     * 获取系统类型作品
     * @param reading_id int 书单id
     */
    public function getResourceReadingListBookId($reading_id)
    {
        return $this->where('reading_id', $reading_id)->pluck('resource_id')->toArray();
    }
}
